<template>

    <div class="login_container">
      <div class="able">
      <h>茶园小区停车场</h></div>
        <div class="login_box">
            <div class="avatar_box">
                <!--头像-->
                <img src="../assets/img_4.png" class="logintp" alt />

            </div>
            <!--添加表单-->
            <el-form
                    ref="loginFormRef"
                    :model="loginForm"
                    :rules="loginRules"
                    class="login_form"
                    label-width="0px">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icondenglu"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="iconfont iconmima" type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
<!--                    <el-button type="info" @click="resetLoginForm">重置</el-button>-->
                  <el-button type="primary" @click="addDialogVisible = true">注册</el-button>
                </el-form-item>
            </el-form>
          <!-- 创建新用户对话框 -->
          <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%"
                     @close="addDialogClosed">
            <!-- 内容主体区域 -->
            <el-form :model="addForm" :rules="addFormRules"
                     ref="addFormRef" label-width="70px">
              <!-- 用户名 -->
              <el-form-item label="用户名" prop="username">
                <el-input v-model="addForm.username"></el-input>
              </el-form-item>
              <!-- 密码 -->
              <el-form-item label="密码" prop="password">
                <el-input v-model="addForm.password"></el-input>
              </el-form-item>
              <!-- 邮箱 -->
              <el-form-item label="车牌号" prop="carnumber">
                <el-input v-model="addForm.carnumber"></el-input>
              </el-form-item>
            </el-form>
            <!-- 内容底部区域 -->
            <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
          </el-dialog>
        </div>
    </div>

</template>

<script>
    export default {
        data() {
            return {
              addDialogVisible: false,
                loginForm: {
                    // username: "admin",
                    // password: "123456"
                  username: "a",
                  password: "1"
                },
              // 添加用户表单项
              addForm: {
                username:'',
                password:'',
                email:'',
              },
              editForm:{},
              // 验证规则
              addFormRules:{
                // 添加用户表单验证规则
                username:[
                  { required: true, message: "请输入用户名", trigger: "blur" },
                  { min: 1, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
                ],
                password:[
                  { required: true, message: "请输入密码", trigger: "blur" },
                  { min: 1, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
                ],
                carnumber:[
                  { required: true, message: "请输入车牌号", trigger: "blur" },
                  { min: 1, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
                ],
              },
                loginRules: {
                    username: [
                        { required: true, message: "请输入用户名", trigger: "blur" },
                        { min: 1, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
                    ],
                    password: [
                        { required: true, message: "请输入密码", trigger: "blur" },
                        { min: 1, max: 8, message: "密码为 6~8 位", trigger: "blur" }
                    ]
                }
            };
        },
        methods: {
          async addUser(){
            this.$refs.addFormRef.validate(async valid=>{
              if( !valid ) return;
              // 发起请求
              const {data:res} = await this.$http.post("addUser",this.addForm);
              if (res != "success") {
                userinfo.state = !userinfo.state;
                return this.$message.error("操作失败！！！");
              }
              this.$message.success("操作成功！！！");
              //隐藏
              this.addDialogVisible = false;
              this.getUserList();
            })
          },
          async addDialogClosed(){
            this.$refs.addFormRef.resetFileds();


          },
            resetLoginForm() {
                this.$refs.loginFormRef.resetFields();
            },
            login() {
                this.$refs.loginFormRef.validate(async valid => {
                    if (!valid) return;
                    // 调用get请求
                    const {data :res} = await this.$http.post("login", this.loginForm);
                //    const {data:rem}=await this.$http.get("alluser",{params:this.queryInfo});
                    console.log(res.flag)
                  console.log(res.user)
                   // console.log("rem")

                    if (res.flag == "ok3" ) {
                        window.sessionStorage.setItem('flag','ok'); // session 放置
                        this.$message.success("登陆成功！！！");
                        this.$router.push({ path: "/test"});
                    }
                    if(res.flag == "ok2"){
                    window.sessionStorage.setItem('flag','ok'); // session 放置
                    this.$message.success("登陆成功112！！");
                    this.$router.push({ path: "/firstmain",query:{carnumber:res.user}});

                    }
                  if(res.flag == "error"){
                    this.$message.error("账号或密码错误登录失败！！！");
                  }
                });
            }
        }
    };
</script>

<style lang="less" scoped>


    .login_container {
   background-color: cadetblue;
      height: 100%;
    }
    .logintp{
      height: 200px;
      width: 250px;
    }
  .login_box{
      position: absolute;
      top: 30%;
      left: 30%;
    background-image: url("../assets/img_5.png");
      width: 600px;
  }
.bodd{
  background: url("../assets/11.png") no-repeat;
}
.able{
  font-size: 60px;
  text-align: center;
  padding-top: 100px;
padding-left: 0px;
 color: bisque;
}
</style>
